// @formatter:off
/**
 * @file c.dor.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-04 03:02:53 +0000 (Tue, 04 Jun 2013) $
 *
 * $Id: c.dor.js 68 2013-06-04 03:02:53Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/extensions/DirectoryOrganizer/templates/c.dor.js $
 */
// @formatter:on

var DOR_autoTagTransID = 0;
var DOR_autoDirTransID = 0;
var DOR_autoCollTransID = 0;

function DOR_CreateAction(form) {
	var ok = true;

	if(ok && $("#ACTION_NAME").val() == "") {
		alert("You must enter a name for the new action");
		$("#ACTION_NAME").focus();
		ok = false;
	}
	if(ok && $("#ACTION_MODE").val() == "") {
		alert("You must select a DirectoryOrganizer mode");
		$("#ACTION_MODE").focus();
		ok = false;
	}

	return ok;
}

function DOR_Cancel(rule) {
	DOR_Done(rule);
}

function DOR_Done(rule) {
	document.location.href = "?action=<:VAR:/>WC_ACTION_RULE_SHOW<::/>&group=<:VAR:/>GROUP-ID<::/>&rule=" + rule;
}

function DOR_loadSelectableCollections() {
	$(".DOR_SelectableCollections").bind("keydown", function(event) {
		if(event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
			event.preventDefault();
		}
	}).autocomplete({
		source: function(request, response) {
			var pattern = request.term.split(/,\s*/).pop();
			if(pattern.length == 0) {
				return false;
			}

			console.log("loadSelectableCollections(): Asking for pattern '" + pattern + "'");

			DOR_autoCollTransID++;
			console.log("loadSelectableCollections(): Sending transaction '" + DOR_autoCollTransID + "'");
			jQuery.get("service.php?action=<:VAR:/>WC_SERVICE_COLLECTIONS<::/>&group=<:VAR:/>GROUP-ID<::/>&pattern=" + pattern + "&transaction=" + DOR_autoCollTransID, function(data) {
				var xml = $($.parseXML(data));
				var transId = xml.find("wc-transaction").text();
				console.log("loadSelectableCollections(): Receiving transaction '" + transId + "'");
				if(transId == DOR_autoCollTransID) {
					console.log("loadSelectableCollections(): Setting results from transaction '" + transId + "'");
					if(xml.find("wc-success").length) {
						var availableTags = Array();

						xml.find("wc-result>collection>path").each(function() {
							availableTags.push($(this).text());
						});
						console.log("loadSelectableCollections(): collections received: '" + availableTags.length + "'");

						response(availableTags);
					} else if(xml.find("wc-failed").length) {
						//var	error = "";
						//if(xml.find("wc-error>wc-code").length) {
						//	error = "[" + xml.find("wc-error>wc-code").text() + "] ";
						//}
						//error = error + xml.find("wc-error>wc-message").text();

						//$("#EDITTAGS_LIST").text("Failed: " + error);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = this.value.split(/,\s*/);
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push(ui.item.value);
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});
}

function DOR_loadSelectableDirectories() {
	$(".DOR_SelectableDirectories").bind("keydown", function(event) {
		if(event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
			event.preventDefault();
		}
	}).autocomplete({
		source: function(request, response) {
			var pattern = request.term.split(/,\s*/).pop();
			if(pattern.length == 0) {
				return false;
			}

			console.log("loadSelectableDirectories(): Asking for pattern '" + pattern + "'");

			DOR_autoDirTransID++;
			console.log("loadSelectableDirectories(): Sending transaction '" + DOR_autoDirTransID + "'");
			jQuery.get("service.php?action=<:VAR:/>WC_SERVICE_DIRECTORIES<::/>&group=<:VAR:/>GROUP-ID<::/>&pattern=" + pattern + "&transaction=" + DOR_autoDirTransID, function(data) {
				var xml = $($.parseXML(data));
				var transId = xml.find("wc-transaction").text();
				console.log("loadSelectableDirectories(): Receiving transaction '" + transId + "'");
				if(transId == DOR_autoDirTransID) {
					console.log("loadSelectableDirectories(): Setting results from transaction '" + transId + "'");
					if(xml.find("wc-success").length) {
						var availableTags = Array();

						xml.find("wc-result>directory>path").each(function() {
							availableTags.push($(this).text());
						});
						console.log("loadSelectableDirectories(): directories received: '" + availableTags.length + "'");

						response(availableTags);
					} else if(xml.find("wc-failed").length) {
						//var	error = "";
						//if(xml.find("wc-error>wc-code").length) {
						//	error = "[" + xml.find("wc-error>wc-code").text() + "] ";
						//}
						//error = error + xml.find("wc-error>wc-message").text();

						//$("#EDITTAGS_LIST").text("Failed: " + error);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = this.value.split(/,\s*/);
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push(ui.item.value);
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});
}

function DOR_loadSelectableTags() {
	$(".DOR_SelectableTags").bind("keydown", function(event) {
		if(event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
			event.preventDefault();
		}
	}).autocomplete({
		source: function(request, response) {
			var pattern = request.term.split(/,\s*/).pop();
			if(pattern.length == 0) {
				return false;
			}

			console.log("loadSelectableTags(): Asking for pattern '" + pattern + "'");

			DOR_autoTagTransID++;
			console.log("loadSelectableTags(): Sending transaction '" + DOR_autoTagTransID + "'");
			jQuery.get("service.php?action=<:VAR:/>WC_SERVICE_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&pattern=" + pattern + "&transaction=" + DOR_autoTagTransID, function(data) {
				var xml = $($.parseXML(data));
				var transId = xml.find("wc-transaction").text();
				console.log("loadSelectableTags(): Receiving transaction '" + transId + "'");
				if(transId == DOR_autoTagTransID) {
					console.log("loadSelectableTags(): Setting results from transaction '" + transId + "'");
					if(xml.find("wc-success").length) {
						var availableTags = Array();

						xml.find("wc-result>tag>name").each(function() {
							availableTags.push($(this).text());
						});
						console.log("loadSelectableTags(): tags received: '" + availableTags.length + "'");

						response(availableTags);
					} else if(xml.find("wc-failed").length) {
						//var	error = "";
						//if(xml.find("wc-error>wc-code").length) {
						//	error = "[" + xml.find("wc-error>wc-code").text() + "] ";
						//}
						//error = error + xml.find("wc-error>wc-message").text();

						//$("#EDITTAGS_LIST").text("Failed: " + error);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = this.value.split(/,\s*/);
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push(ui.item.value);
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});
}

$(function() {
	DOR_loadSelectableTags();
	DOR_loadSelectableDirectories();
	DOR_loadSelectableCollections();
});
